<?php
/* --------- EPL 0.1 Global Class 0.1 --------------
Created: 4/22/2007 10:22:13 PM
Contact i_farid_i@yahoo.com to obtain permission
Copyright �2007 some rights reserved
-------------------------------------------------- */

class GlobalLib
{
	var $_LIB_VAR_LIST = Array();
	var $_LIB_VAR_ERR = Array();
	var $_LIB_VAR_MSG = Array();
	var $_LIB_VAR_STATUS = Array();

	var $_date_string = array(
	"id" => Array(
		"day" => Array(
			0 => "Minggu",
			1 => "Senin",
			2 => "Selasa",
			3 => "Rabu",
			4 => "Kamis",
			5 => "Jum'at",
			6 => "Sabtu"),
		"month" => Array(
			1 => "Januari",
			2 => "Februari",
			3 => "Maret",
			4 => "April",
			5 => "Mei",
			6 => "Juni",
			7 => "Juli",
			8 => "Agustus",
			9 => "September",
			10 => "Oktober",
			11 => "November",
			12 => "Desember"),
		"d" => Array(
			0 => "Aha",
			1 => "Sen",
			2 => "Sel",
			3 => "Rab",
			4 => "Kam",
			5 => "Jum",
			6 => "Sab"),
		"m" => Array(
			1 => "Jan",
			2 => "Feb",
			3 => "Mar",
			4 => "Apr",
			5 => "Mei",
			6 => "Jun",
			7 => "Jul",
			8 => "Agu",
			9 => "Sep",
			10 => "Okt",
			11 => "Nov",
			12 => "Des")
	),

	"en" => Array(
		"day" => Array(
			0 => "Sunday",
			1 => "Monday",
			2 => "Tuesday",
			3 => "Wednesday",
			4 => "Thursday",
			5 => "Friday",
			6 => "Saturday"),
		"month" => Array(
			1 => "January",
			2 => "February",
			3 => "March",
			4 => "April",
			5 => "May",
			6 => "June",
			7 => "July",
			8 => "August",
			9 => "September",
			10 => "October",
			11 => "November",
			12 => "December"),
		"d" => Array(
			0 => "Sun",
			1 => "Mon",
			2 => "Tue",
			3 => "Wed",
			4 => "Thu",
			5 => "Fri",
			6 => "Sat"),
		"m" => Array(
			1 => "Jan",
			2 => "Feb",
			3 => "Mar",
			4 => "Apr",
			5 => "May",
			6 => "Jun",
			7 => "Jul",
			8 => "Aug",
			9 => "Sep",
			10 => "Oct",
			11 => "Nov",
			12 => "Dec"),
	)
	);

	var $_language = "en";

	/**
	* Set default language for any localization string
	*
	* @return void
	* @param string $langstr
	*/
	function set_language($langstr)
	{
		$this->_language = $langstr;
	}

	/**
	* Get values from GET/POST request string
	*
	* @return void
	* @param string $varname
	*/
	function request($varname)
	{
                
		if ( isset($_GET[$varname]) )
		{
			return $_GET[$varname];
		}
		else if ( isset($_POST[$varname]) )
		{
			return $_POST[$varname];
		}
		else
		{
			return NULL;
		}
	}

	/**
	* To redirect current page to another
	*
	* @return void
	* @param string $targeturl
	*/
	function redirect($targeturl)
	{
		header("Location: " . $targeturl);
		exit;
	}

	/**
	* Escape all string with \
	*
	* @return string
	* @param string $str
	*/
	function _escape_it($str)
	{
		$export = '';
		for ( $i=0; $i<strlen($str); $i++ )
		{
			$export .= "\\" . $str{$i};
		}

		return $export;
	}

	/**
	* Get a localized formatted date string based on standard unix
	* timestamp (instead of using standard date() function)
	*
	* @return string
	* @param int[optional] $thedate
	* @param int[optional] $theformat
	*/
	function date($theformat = NULL, $thedate = NULL)
	{
		// first, get the date array based on the timestamp
		if ( is_null($thedate) )
		{
			$thedate = getdate();
		}
		else
		{
			$thedate = getdate($thedate);
		}

		if ( is_null($theformat) ) $theformat = "d/m/Y";

		$export = '';
		for ( $zz=0; $zz<strlen($theformat); $zz++ )
		{
			switch ( $theformat{$zz} )
			{
				case "l": $export .= $this->_escape_it($this->_date_string[$this->_language]["day"][$thedate["wday"]]);break;
				case "D": $export .= $this->_escape_it($this->_date_string[$this->_language]["d"][$thedate["wday"]]);break;
				case "F": $export .= $this->_escape_it($this->_date_string[$this->_language]["month"][$thedate["mon"]]);break;
				case "M": $export .= $this->_escape_it($this->_date_string[$this->_language]["m"][$thedate["mon"]]);break;
				default: $export .= $theformat{$zz};
			}
		}
		$theformat = $export;

		return date($theformat, $thedate[0]);
	}

	function get_timestamp($tanggal, $mode='-') {
		list($hr, $mn, $sc) = explode(":",$tanggal);
		switch ($mode) {
	    case '/': list($tgl, $bln, $thn) = explode("/",$tanggal);break;
		case '-': list($thn, $bln, $tgl) = explode("-",$tanggal);break;
		default: list($tgl, $bln, $thn) = explode("/",$tanggal);break;
		}

	    return mktime($hr, $mn, $sc, $bln, $tgl, $thn);
	}

	/* ==========================
	ERROR AND MESSAGE MANAGEMENT
	===========================*/
	function isErr()
	{
		if ( count($this->_LIB_VAR_ERR) > 0 )
		{
			return true;
		} else {
			return false;
		}
	}

	function isMsg()
	{
		if ( count($this->_LIB_VAR_MSG) > 0 )
		{
			return true;
		} else {
			return false;
		}
	}

	function addErr($msgError) {
	    if ($msgError!='') {
			$this->_LIB_VAR_ERR[] = $msgError;
	    }
	}

	function addMsg($msgText) {
	    if ($msgText!='') {
	        $this->_LIB_VAR_MSG[] = $msgText;
	    }
	}

	function verifyErr($cekname)
	{
		if ( $this->checkErr($cekname) ) {
			return "<span class=error>Required</span>";
		} else {
			return "";
		}
	}

	function fieldMark($cekname, $status = "required")
	{
		$this->_LIB_VAR_STATUS[$cekname] = $status;
	}

	function fieldErr($cekname, $status = "required", $awalan = true)
	{
		$this->addErr(($awalan?$cekname . " ":"") . $status);
		$this->fieldMark($cekname, $status);
		$this->_LIB_VAR_LIST[] = $cekname;
	}

	function fieldStatus($cekname)
	{
		if ( array_key_exists($cekname, $this->_LIB_VAR_STATUS) )
		{
			return '<span class="error">' . $this->_LIB_VAR_STATUS[$cekname] . '</span>';
		} else return NULL;
	}

	function errorStatus()
	{
		if ( count($this->_LIB_VAR_LIST) > 0 ) {
			if ( $this->isErr() )
			{
				$errMsg .= '<div class="pesan salah"><ul>';
				foreach ($this->_LIB_VAR_ERR as $msg) $errMsg .= '<li>' . $msg . '</li>';
				$errMsg .= '</ul></div>';
			}
		}

		return $errMsg;
	}

	function messageStatus()
	{
		if ( count($this->_LIB_VAR_LIST) == 0 ) {
			if ( $this->isMsg() )
			{
				$errMsg .= '<div class="pesan info"><ul>';
				foreach ($this->_LIB_VAR_MSG as $msg) $errMsg .= '<li>' . $msg . '</li>';
				$errMsg .= '</ul></div>';
			}
		}

		return $errMsg;
	}

	/* ===============================
	DATA VALIDATION
	================================ */
	function validateUsername($uname) {
	    return preg_match("/^([A-Za-z0-9]{4,15})$/i", $uname);
	}

	function validateEmail($email) {
	    return preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@([0-9a-z](-?[0-9a-z])*\.)+[a-z]{2}([zmuvtg]|fo|me)?$/i",$email);
	}

	function validateDate($date) {
	    return preg_match("/^([0-9]{1,2})-([0-9]{1,2})-([0-9]{2,4})$/i", $date);
	}

	function validatePassword($pass) {
	    return preg_match("/^([A-Za-z0-9]{4,20})$/i", $pass);
	}

	function validateName($name) {
	    return preg_match("/^([A-Za-z�-��-��-� \.,]{2,})$/i", $name);
	}

	function validateInteger($val) {
            //echo "@validate integer ". $out = preg_match("^([0-9])$", $val);
	    return preg_match("/^([0-9])$/i", $val);
	}

}


/* STRING PARSER */

function nl2br_pre($string, $wrap = 40) {
	$string = nl2br($string);
	$str = "? >"; // ini perlu didempetkan untuk productions
	preg_match_all("/<pre[^>]*". $str ."(.|\n)*?<\/pre>/", $string, $pre1);
	for ($x = 0; $x < count($pre1[0]); $x++) {
		$pre2[$x] = preg_replace("/\s*<br[^>]*". $str ."\s*/", "", $pre1[0][$x]);
		$pre2[$x] = preg_replace("/([^\n]{".$wrap."})(?!<\/pre>)(?!\n)/", "$1\n", $pre2[$x]);
		$pre1[0][$x] = "/".preg_quote($pre1[0][$x], "/")."/";
	}
	return preg_replace($pre1[0], $pre2, $string);
}

function htmlEncodeText($string)
{
	$pattern = '<([a-zA-Z0-9\. "\'_\/-=;\(\)?&#%]+)>';
	preg_match_all ('/' . $pattern . '/', $string, $tagMatches, PREG_SET_ORDER);
	$textMatches = preg_split ('/' . $pattern . '/', $string);
	foreach ($textMatches as $key => $value) {
	$textMatches [$key] = htmlentities ($value);
	}
	for ($i = 0; $i < count ($textMatches); $i ++) {
		$textMatches [$i] = $textMatches [$i] . $tagMatches [$i] [0];
	}
	return implode ($textMatches);
}

function getCurrentBalance($idoutlet){
	global $dbi;
	$SQL = "SELECT balance FROM mt_outlet WHERE id_outlet = '".$idoutlet."'";
	$res = $dbi->query($SQL);
	return $res[1]->balance;
}
?>